package com.smile.stream;

import java.util.ArrayList;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2021/10/7 14:48
 */
public class ReduceDemo {
    public static void main(String[] args) {
        reduceTest1();
        reduceTest2();
        reduceTest3();
        System.out.println("$$$$$$");
        max();
        min();
    }

    private static void min() {
        Optional<Integer> min = Stream.of(1,2,3,4).reduce(Integer::min);
        System.out.println(min);
    }

    private static void max() {
        Optional<Integer> max = Stream.of(1,2,3,4).reduce(Integer::max);
        System.out.println(max);
    }

    public static void reduceTest1() {
        System.out.println("##############################################################################");
        Optional accResult = Stream.of(1,2,3,4).reduce((acc,item) -> {
            System.out.println("acc : " + acc);
            acc += item;
            System.out.println("item: " + item);
            System.out.println("acc+ : " + acc);
            System.out.println("--------");
            return acc;
        });
        System.out.println(accResult.get());
    }


    public static void reduceTest2() {
        System.out.println("##############################################################################");
        Integer reduce = Stream.of(1,2,3,4).reduce(100,(acc,item) -> {
            System.out.println("acc : " + acc);
            acc += item;
            System.out.println("item: " + item);
            System.out.println("acc+ : " + acc);
            System.out.println("--------");
            return acc;
        });
        System.out.println(reduce);
    }
    public static void reduceTest3() {
        System.out.println("##############################################################################3");
        ArrayList<Integer> newList = new ArrayList<>();
        ArrayList<Integer> reduce = Stream.of(1,2,3,4).reduce(newList,(acc,item) -> {
            System.out.println("acc : " + acc);
            acc.add(item);
            System.out.println("item: " + item);
            System.out.println("acc+ : " + acc);
            System.out.println("--------");
            return acc;
        },(acc,item) -> null);
        System.out.println(reduce);
    }
}
